package valueobject

import (
	"regexp"
	"strings"

	"BookStack/pkg/errors"
)

// ValidateEmail 验证邮箱格式
func ValidateEmail(email string) error {
	email = strings.TrimSpace(email)
	if email == "" {
		return errors.New(errors.ErrCodeEmailFormat, "")
	}

	// 使用正则表达式验证邮箱格式
	pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
	matched, err := regexp.MatchString(pattern, email)
	if err != nil {
		return errors.New(errors.ErrCodeSystem, "")
	}
	if !matched {
		return errors.New(errors.ErrCodeEmailFormat, "")
	}

	return nil
}
